package com.wanglin.jetpacktest

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel

class MainViewModel(countReserved:Int):ViewModel() {

    private val _counter=MutableLiveData<Int>()

    val counter:LiveData<Int> =Transformations.map(_counter){
        it
    }


    init {
        _counter.value=countReserved
    }

    fun plusOne()
    {
        val count= _counter.value?:0
        _counter.value= count+1
    }

    fun clear()
    {
        _counter.value=0
    }

    private val userIdLiveData=MutableLiveData<String>()
    val user:LiveData<User> =Transformations.switchMap(userIdLiveData){
        Repository.getUser(it)
    }



    fun getUser(userId:String)
    {
         userIdLiveData.value=userId
    }
}